package net {

	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import view.audioPlayer;


	public class ConnectionManager extends EventDispatcher{		

		private static var connectionManager:ConnectionManager = null;
		private var conn:NetConnection = null;

		/**
		 * Constructor
		 **/

		public function ConnectionManager() {
		}

		/**
		 * Static getInstance returns a singleton ConnectionManager
		 **/
		public static function getInstance() : ConnectionManager{

			if(connectionManager == null){
				return new ConnectionManager();
			}

			return connectionManager;
		}

		/**
		 * connects to server
		 **/

	 
		public function connect(url:String, user:String) : void {

			if(conn == null) {
				conn = new NetConnection();
				if (user != null){
					conn.connect(url,user);
				}
				else conn.connect(url);
				conn.addEventListener(NetStatusEvent.NET_STATUS, onConnNetStatus);
				//conn.addEventListener(Event.ACTIVATE, onConnActivate);
				//conn.addEventListener(Event.DEACTIVATE, onConnDeactivate);
				//conn.addEventListener(IOErrorEvent.IO_ERROR, onConnIOError);
				//conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onConnSecurityError);
			}
			return;
		}
		/**
		 * Gets the connection object
		 * 
		 * @return conn NetConnection
		 **/

		public function getConnection() : NetConnection{
			return conn;
		}

		/**
		 * called after NetStatusEvent is dispatched
		 * 
		 * @param event NetStatusEvent
		 **/

		public function onConnNetStatus(event:NetStatusEvent) : void {			

			// dispatch event
			this.dispatchEvent(event);
		}

	}

}